<template>
  <custom-dialog
    ref="dialog" :title="modalTitle" height="500px" width="80%"
    :before-confirm="beforeConfirm" :on-confirm="onConfirm"
  >
    <el-form ref="form" :model="info" :rules="rules" label-width="120px" class="form-box">
      <el-form-item label="计量器具编号" prop="toolsNumber">
        <el-input v-model="info.toolsNumber"></el-input>
      </el-form-item>
      <el-form-item label="上级计量器具编号">
        <el-input v-model="info.superiorToolsNumber"></el-input>
      </el-form-item>
      <el-form-item label="计量器具名称" prop="toolsName">
        <el-input v-model="info.toolsName"></el-input>
      </el-form-item>
      <el-form-item label="计量器具类型">
        <measure-tools-type-picker v-model="info.toolsType" filterable clearable></measure-tools-type-picker>
      </el-form-item>
      <el-form-item label="能源品种">
        <energy-variety-picker v-model="info.energyVariety" filterable clearable></energy-variety-picker>
      </el-form-item>
      <el-form-item label="计量等级">
        <el-input v-model="info.measurementLevel"></el-input>
      </el-form-item>
      <el-form-item label="类型规格">
        <el-input v-model="info.typeSpecifications"></el-input>
      </el-form-item>
      <el-form-item label="生产厂家">
        <el-input v-model="info.manufacturer"></el-input>
      </el-form-item>
      <el-form-item label="精准度等级">
        <el-input v-model="info.accuracyClass"></el-input>
      </el-form-item>
      <el-form-item label="测量范围">
        <el-input v-model="info.measuringRange"></el-input>
      </el-form-item>
      <el-form-item label="检定结果">
        <el-input v-model="info.testResult"></el-input>
      </el-form-item>
      <el-form-item label="检定机构">
        <el-input v-model="info.testInstitution"></el-input>
      </el-form-item>
      <el-form-item label="检定周期">
        <el-input v-model="info.testCycle"></el-input>
      </el-form-item>
      <el-form-item label="上次检定时间">
        <el-input v-model="info.lastTestTime"></el-input>
      </el-form-item>
      <el-form-item label="下次检定时间">
        <el-input v-model="info.nextTestTime"></el-input>
      </el-form-item>
      <el-form-item label="未检定原因">
        <el-input v-model="info.untestedReason"></el-input>
      </el-form-item>
      <el-form-item label="安装位置">
        <el-input v-model="info.installationLocation"></el-input>
      </el-form-item>
      <el-form-item label="安装厂家">
        <el-input v-model="info.installationManufacturer"></el-input>
      </el-form-item>
      <el-form-item label="安装时间">
        <el-input v-model="info.installationTime"></el-input>
      </el-form-item>
      <el-form-item label="接入系统">
        <el-input v-model="info.accessSystem"></el-input>
      </el-form-item>
      <el-form-item label="接入状态">
        <el-input v-model="info.accessStatus"></el-input>
      </el-form-item>
      <el-form-item label="运行状态">
        <el-input v-model="info.runningState"></el-input>
      </el-form-item>
      <el-form-item label="状态发生时间">
        <el-input v-model="info.stateOccurrenceTime"></el-input>
      </el-form-item>
      <el-form-item label="更换时间">
        <el-input v-model="info.replacementTime"></el-input>
      </el-form-item>
      <el-form-item label="清零时间">
        <el-input v-model="info.clearTime"></el-input>
      </el-form-item>
    </el-form>
  </custom-dialog>
</template>

<script>
import CustomDialog from '@/components/custom-dialog'
import { saveOrUpdate } from '@/api/measuringTools'
import MeasureToolsTypePicker from '@/components/measure-tools-type-picker'
import EnergyVarietyPicker from '@/components/energy-variety-picker'

const api = {
  saveOrUpdate,
}
export default {
  name: 'operate-modal',
  components: { EnergyVarietyPicker, MeasureToolsTypePicker, CustomDialog },
  props: {
    modalTitle: String,
    modalData: {
      type: Object,
      default () {
        return {}
      }
    },
  },
  data () {
    return {
      info: {
        // ID
        id: '',
        // 计量器具编号
        toolsNumber: '',
        // 上级计量器具编号
        superiorToolsNumber: '',
        // 计量器具名称
        toolsName: '',
        // 计量器具类型
        toolsType: '',
        // 能源品种
        energyVariety: '',
        // 计量等级
        measurementLevel: '',
        // 类型规格
        typeSpecifications: '',
        // 生产厂家
        manufacturer: '',
        // 精准度等级
        accuracyClass: '',
        // 测量范围
        measuringRange: '',
        // 检定结果
        testResult: '',
        // 检定机构
        testInstitution: '',
        // 检定周期
        testCycle: '',
        // 上次检定时间
        lastTestTime: '',
        // 下次检定时间
        nextTestTime: '',
        // 未检定原因
        untestedReason: '',
        // 安装位置
        installationLocation: '',
        // 安装厂家
        installationManufacturer: '',
        // 安装时间
        installationTime: '',
        // 接入系统
        accessSystem: '',
        // 接入状态
        accessStatus: '',
        // 运行状态
        runningState: '',
        // 状态发生时间
        stateOccurrenceTime: '',
        // 更换时间
        replacementTime: '',
        // 清零时间
        clearTime: '',
      },
      rules: {
        toolsNumber: [{ required: true, message: '必填项' }],
        toolsName: [{ required: true, message: '必填项' }],
      },
    }
  },
  methods: {
    open () {
      this.info = {
        ...this.$options.data().info,
        ...this.modalData
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      this.$nextTick(() => {
        this.$refs.dialog.open()
      })
    },
    beforeConfirm () {
      return this.$refs.form.validate()
    },
    onConfirm (close) {
      const params = { ...this.info }
      return api.saveOrUpdate(params).then(res => {
        if (res.code === 200) {
          this.$emit('save-success')
          this.$message.success('保存成功')
          close()
        }
      })
    },
  },
}
</script>

<style scoped>

</style>
